<?php

namespace app\admin\controller;


use app\common\model\ShopMember;
use app\common\model\ShopMemberCard;
use app\common\model\ShopMemberLevel;
use app\common\service\Status;
use buildView\Button;
use buildView\Detail;
use buildView\Filter;
use think\Controller;
use buildView\Form;
use buildView\Grid;
use app\common\model\User;
use Endroid\QrCode\QrCode;
use app\shop\service\Order;


    /**
    * 会员管理
    * Class Member
    * @package app\admin\controller
    */
class Member extends Controller
{
    /**
     * 列表
     * @auth true
     * @menu true
     * @return string
     */
    public function index()
    {
        $store = session('store');
        $grid = new Grid(new User);
         if ($store){
             $grid->model()->where('store_id',$store['id']);
         }else{
            $grid->model();
         }
        $grid->id('卡号');
        $grid->headimg('会员')->display(function ($val, $data, $html) {
            return $html . '<br>' . $data['nickname'];
        })->setExcelData(function($val,$data){
            return $data['nickname'];
        })->image(50);
        $grid->phone('手机号码');
//        $grid->gender('性别')->using(Status::SEXSTATUS())->badge();
//        $grid->birthday('生日');

        $grid->usermember('会员卡信息')->display(function ($vals, $data) {
            $htmlArr = [];
            foreach ($vals as $val) {
                $syu = $val['total_num'] - $val['use_num'];
                $htmlArr[] = "<p>{$val['card_name']}</p><p class='color-desc'>总数：{$val['total_num']} 件 &nbsp--&nbsp; 剩余：{$syu}件</p>";
            }
            $html = implode('<hr>', $htmlArr);
            return $html;
        })->align('left');

        $grid->create_at('注册时间');
        $grid->setFromOpen();
        // $grid->hideAddButton();
        $grid->hideDeletesButton();
        $grid->filter(function(Filter $filter){
           $filter->like('nickname','姓名');
           $filter->like('phone','手机号码');
           $filter->dateBetween('create_at','注册时间');
        });
        $grid->actions(function($action,$data){
            $action->hideDel();
            // $action->hideEdit();
            $button = new Button('会员卡信息');
            $button->href(url('admin/MemberInfo/index') . "?id={$data['id']}");
            $action->append($button);
            $button2 = new Button('钱包明细','normal');
            $button2->href(url('money_record/index') . "?id={$data['id']}");
            $action->append($button2);
        });
        return $grid->view();
    }

    /**
     * 详情
     * @auth true
     * @menu true
     * @return string
     */
    public function detail()
    {
        $detail = new Detail(new User);
        $detail->setTitle('会员信息');
        $data = $detail->getModelData();
        $detail->layout('基本信息', 6, function ($detail) {
            $detail->name('姓名');
            $detail->nickname('昵称');
            $detail->headimg('头像')->image();
            $detail->gender('性别')->using(Status::SEXSTATUS())->badge();
            $detail->level()->title('年级')->badge();
            $detail->user_class('班级');
            $detail->phone('手机号');
            $detail->birthday('生日');
            $detail->home_address('家庭住址');
        });
        $detail->layout('账户信息', 6, function ($detail) {
            $detail->money('账户余额');
        });
        $store = session('store');
        if (!$store){
            $detail->layout('门店信息', 6, function ($detail) {
                $detail->store()->name('现所属门店')->badge();
                $detail->originalStore()->name('所属地');
            });
        }
        $detail->layout('二维码', 6, function ($detail) {
            
            // $detail->img('头像')->image(function ($img){
            //     return "<img src='.$img.'/>";
            // });
            $detail->img('会员码')->display(function ($val, $data, $html) {
                $qrCode = new QrCode($data['id']);
                header('Content-Type: '.$qrCode->getContentType());
                $img = $qrCode->writeDataUri();$img;
                return "<img src='".$img."'/>";
            });
            
        });
        return $detail->view();
    }

    /**
     * 会员卡信息
     * @auth true
     * @menu true
     * @return string
     */
    public function memberInfo()
    {
        $id = $this->request->param('id');
        $grid = new Grid(new ShopMember);
        $grid->model()->where('uid',$id);
        $grid->card()->name('会员卡名称');
        $grid->status('状态')->using(Status::CARDSTATUS())->badge();
        $grid->total_num('可洗衣总次数');
        $grid->use_num('已使用次数');
        $grid->residue_num('剩余次数');
        $grid->free_num('每月免费次数');
        $grid->free_use_num('已使用免费次数');
        $grid->expire_card('到期时间');

        $grid->setFromOpen();
        //$grid->hideAction();
        $grid->hideAddButton();
        $grid->hideExportButton();
        $grid->hideDeletesButton();
        $grid->actions(function($action,$data){
            $action->hideDel();
            $action->hideEdit();
            $action->hideDetail();
            $button = new Button('编辑次数');
            $button->href(url('editCount') . "?id={$data['id']}", 'modal');
            $action->append($button);
        });
        return $grid->view();
    }

    public function editCount(){
        //$id = $this->request->param('id');
        $form = new Form(new ShopMember);
        $form->text('total_num','总次数');
        $form->text('use_num','已清洗次数');

        $form->saved(function($post,$model){
            file_put_contents('log_admin_change_user.txt', date('Y-m-d H:i:s').' '.json_encode($post,JSON_UNESCAPED_UNICODE) . "\r\n", FILE_APPEND | LOCK_EX);

            (new ShopMember)->saveAll($post);
            return $post;
        });
        return $form->view();
    }

    /**
     * 添加/编辑
     * @auth true
     * @menu true
     * @return string
     */
    public function form()
    {
        $store = session('store');
        $user = new User();
        $form = new Form($user);
        $form->hidden('original_store_id','所属地门店')->default($store['id']);
        $form->hidden('store_id','店铺id')->default($store['id']);
		$form->text('name','姓名')->required();
		$form->text('nickname','会员昵称')->required();
		$form->text('phone','手机号');
		$form->text('address','详细地址');
        $form->select('gender', '性别')->options(Status::SEXSTATUS())->default(1);
        $memberLevel = ShopMemberCard::select()->column('name','id');
//        $form->select('member_card', '会员卡')->options($memberLevel)->default(1);

        //file_put_contents('log_admin_change_user.txt', json_encode($user->member()) . "\r\n", FILE_APPEND | LOCK_EX);

        $form->saved(function($post,$model){

            file_put_contents('log_admin_change_user.txt', date('Y-m-d H:i:s').' '.json_encode($post,JSON_UNESCAPED_UNICODE) . "\r\n", FILE_APPEND | LOCK_EX);

            (new ShopMember)->saveAll($post);
            return $post;
        });
        return $form->view();
    }
}
